﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Clothes.BLL;
using Clothes.Comment;
using Clothes.IBLL;
using Clothes.Model;
using Clothes.UI.Model;

namespace Clothes.UI
{
    public partial class Login : System.Web.UI.Page
    {
        IAccountManager AccountManager = new AccountManager();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                var result = AccountManager.LoginByUserName(txtUserName.Text, txtPassword.Text);
                // 登录验证
                
                CLUserBLL userManager = new CLUserBLL();
                if (result.StatusCode == Comment.StatusCode.Succeed)
                {

                    // 缓存用户资料
                    var user = result.ReadResult();
                    IdentityManager.SaveUser(user);
                    var url = Request.QueryString["ReturnUrl"];
                    // 脚本跳转
                   
                    if (!string.IsNullOrWhiteSpace(url))
                    {
                        Response.Redirect("#");
                    }
                    else
                    {
                        Response.Redirect("#");
                    }

                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "loginerror", $"alert('{result.Message}')", true);
                }
            }
        }
    }
}